﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smart.Model;
using Smart.Data.CaloriesCalculator;
using Smart.Data;

namespace Smart.Business.CaloriesCalculator
{
    public class CaloriesCalculatorBL
    {
        private CaloriesCalculatorDL dal = null;

        public CaloriesCalculatorBL()
        {
            if (dal == null)
            {
                dal = new CaloriesCalculatorDL(null);
            }
        }

        public CaloriesCalculatorBL(ISmartEntities context)
        {
            if (dal == null)
            {
                dal = new CaloriesCalculatorDL(context);
            }
        }

        public List<MealCategory> GetAllCategories()
        {
            return dal.GetAllCategories();     
        }


        public List<MealTiny> GetAllMeals()
        {
            List<MealTiny> result = new List<MealTiny>();
            foreach (var v in dal.GetAllMeals())
            {
                result.Add(new MealTiny(v.Id, v.MealName, v.Details));
            }

            return result;
        }

        public List<MealTiny> GetAllMealsFromCategory(int catId)
        {
            List<MealTiny> result = new List<MealTiny>();
            foreach (var v in dal.GetAllMealsFromCategory(catId))
            {
                result.Add(new MealTiny(v.Id, v.MealName, v.Details));
            }

            return result;            
        }

        public void RecordMeal(int userId, int mealId, int _quantity)
        {
            CalculatedResult res = CalculateCaloriesForMeal(mealId, _quantity);
            MealLog ml = new MealLog();
            ml.CreatedBy = userId;
            ml.CreatedOn = DateTime.Now;
            ml.MealId = mealId;
            ml.Quantity = _quantity;
            ml.Carbs = res.TotalCarbs;
            ml.Energy = res.TotalCals;
            ml.Proteins = res.TotalProts;
            ml.Fats = res.TotalFats;

            dal.RecordMeal(ml);
        }

        public CalculatedResult CalculateCaloriesForMeal(int mealId, int quantity)
        {
            Meal m = dal.GetMealById(mealId);
            CalculatedResult res = new CalculatedResult(m.Energy * ((double) quantity / 100),
                                                        m.Proteins * ((double) quantity / 100),
                                                        m.Carbs * ((double) quantity / 100),
                                                        m.Fats * ((double) quantity / 100));
            return res;
        }

        public CalculatedResult CalculateCalories(List<KeyValuePair<int, int>> list)
        {
            CalculatedResult resTotal = new CalculatedResult(0,0,0,0);
            foreach (var l in list)
            {
                CalculatedResult resMeal = CalculateCaloriesForMeal(l.Key, l.Value);
                resTotal.TotalCals +=  Math.Round(resMeal.TotalCals,3);
                resTotal.TotalCarbs += Math.Round(resMeal.TotalCarbs,3);
                resTotal.TotalProts += Math.Round(resMeal.TotalProts,3);
                resTotal.TotalFats += Math.Round(resMeal.TotalFats,3);
            }
            return resTotal;
        }
    }
}
